package com.lry.basic.proxy.demo2;

import com.lry.basic.proxy.demo2.anno.Feign;
import com.lry.basic.proxy.demo2.anno.RequestMapping;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestImpl {
    public static void main(String[] args) {

        UserServiceImpl target = new  UserServiceImpl();

        UserService userService = (UserService) Proxy.newProxyInstance(
                UserService.class.getClassLoader(),
                new Class[]{UserService.class},
                new MyInvocationHandler(target));

        userService.queryUserById(1);

    }
    static class MyInvocationHandler implements InvocationHandler {
        UserServiceImpl targe;
        public MyInvocationHandler(UserServiceImpl target) {
            this.targe = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            targe.queryUserById((Integer) args[0]);

            Feign feign = method.getDeclaringClass().getAnnotation(Feign.class);
            String serviceName = feign.serviceName();
            RequestMapping requestMapping =  method.getAnnotation(RequestMapping.class);
            String url = requestMapping.url();
            url = serviceName+url;
            System.out.println(url);
            System.out.println(args[0]);
            return null;
        }
    }
}
